/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.users;

import java.util.List;

import org.apache.log4j.Logger;
import org.myontology.controller.AbstractAction;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.AttributeLevel;
import org.myontology.controller.ValueHandler;
import org.openid4java.consumer.ConsumerManager;
import org.openid4java.discovery.DiscoveryInformation;
import org.openid4java.message.AuthRequest;
import org.openid4java.message.ax.FetchRequest;
import org.openid4java.server.RealmVerifier;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class OpenIDLogin extends AbstractAction {

	public static final ValueHandler<ConsumerManager> CONSUMER_MANAGER = new ValueHandler<ConsumerManager>(AttributeLevel.APPLICATION,OpenIDLogin.class);
	public static final ValueHandler<DiscoveryInformation> DISCOVERY_INFORMATION = new ValueHandler<DiscoveryInformation>(AttributeLevel.SESSION,OpenIDLogin.class);
	public static final ValueHandler<AuthRequest> REQUEST = new ValueHandler<AuthRequest>(AttributeLevel.REQUEST,OpenIDLogin.class);
	
	private Logger logger;
	
	/* (non-Javadoc)
	 * @see org.myontology.controller.Action#execute(org.myontology.controller.AttributeHolder)
	 */
	public void execute(AttributeHolder attributes) throws Exception {
		try {
			ConsumerManager manager = CONSUMER_MANAGER.getValue(attributes);
			if (manager==null) {
				manager = new ConsumerManager();
				CONSUMER_MANAGER.setValue(attributes, manager);
				RealmVerifier rV = new RealmVerifier();
				rV.setEnforceRpId(false);
				manager.setRealmVerifier(rV);
			}
			
			String returnURL = "http://myontology.deri.at/openid/openid_verify.do";
			String userURL = attributes.getRequestParameter("openid_url");
			List discoveries = manager.discover(userURL);
			DiscoveryInformation discovered = manager.associate(discoveries);
			DISCOVERY_INFORMATION.setValue(attributes,discovered);

			AuthRequest authReq = manager.authenticate(discovered, returnURL);
			
			FetchRequest fetch = FetchRequest.createFetchRequest();
			fetch.addAttribute("FirstName", "http://schema.openid.net/namePerson/first", true);
			fetch.addAttribute("LastName", "http://schema.openid.net/namePerson/last", true);
			fetch.addAttribute("Email", "http://schema.openid.net/contact/email", true);
            // attach the extension to the authentication request
            authReq.addExtension(fetch);
            REQUEST.setValue(attributes, authReq);
		} catch (Exception e) {
			
			logger.error(e);
		}
		
	}

}
